package org.feiye.studyday27.tongbuanli;

import java.util.Random;

//取钱人
public class Taker extends Thread {
	private Accout ac;

	public Taker(String name, Accout ac) {
		super(name);
		this.ac = ac;
	}

	@Override
	public void run() {
		Random r = new Random();
		while (true) {
			int money = r.nextInt(500);
			System.out.println("账户余额是：" + ac.getMoney() + "," + super.getName() + "准备取钱：" + money + "元");
			synchronized (ac) {
				while (this.ac.getMoney() < money) {
					System.out.println("余额不足，" + super.getName() + "准备取钱：" + money + "元");
					try {
						ac.wait(50000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				this.ac.withDraw(money);
				System.out.println(super.getName() + "取钱" + money + "元，现在余额是：" + ac.getMoney() + "元");
			}
		}
	}
}
